package zozopo.search;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.TimerTask;

import javax.servlet.ServletContext;



public class StatisticsTask extends TimerTask
{

    private static final int STATISTICS_SCHEDULE_HOUR = 0;
    private static boolean isRunning = false;
    private static boolean isFistTime = true; 
    
    private ServletContext context = null;

    public StatisticsTask(ServletContext context)
    {
        this.context = context;
    }
   
    @Override
    public void run()
    {
        Calendar cal = Calendar.getInstance();
        //System.out.println(isRunning);
        if (!isRunning)
        {
          
                isRunning = true;
                context.log("开始执行指定任务");
               
                //TODO 添加自定义的详细任务
                executeTask();
               
                //指定任务执行结束
                isRunning = false;
                context.log("指定任务执行结束");
        }
        else
        {
            context.log("上一次任务执行还未结束");
        }
   
    }

   
    public void executeTask()
    {
    	if(!isFistTime){
    		System.out.println("执行搜索Index");
            Indexer indexer = new Indexer();
    		try {
    			indexer.index(new File(context.getRealPath("search/index")));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println("执行搜索Index完成");
    	}else{
    		isFistTime = false;
    	}
        
		
		//indexer.releaseConnection();
        
       // System.out.println("任务2");
    }
}
